<?php
/**
 * 多线程基类
 * 传递的参数必须要保证可以序列化,最好不要直接传递对象!!!!
 */
require_once ('tr_base.php');
class threadBase extends Threaded
{
    //开线程子类名
    var $obj_name;
    //主线程传递的数据
    var $data;
    //任务名
    var $task_name;
    //调用任务函数返回的值
    var $tr_result;
    //垃圾回收标记
    private $state;

    function __construct($obj_name,$data,$task_name = false){
        $this->obj_name = $obj_name;
        $this->data = $data;
        $this->task_name = $task_name;
        $this->state = false;
    }

    /**
     * 调用对象中的任务
     */
    function run(){
        //清理start中调用$_SERVER变量的REQUEST_METHOD异常
        $_SERVER['REQUEST_METHOD'] = "script";
        date_default_timezone_set("PRC");
        $this->tr_result = $this->worker->initObj($this->task_name)->task($this);
        $this->setGarbage();
    }

    /**
     * 判断垃圾是否可以回收
     * @return bool|void
     */
    public function isGarbage()
    {
        return $this->state;
    }

    /**
     * 标记垃圾可以回收了
     */
    public function setGarbage()
    {
        //调用worker的垃圾回收标记
        @$this->worker->setGarbage();
        $this->state = true;
    }
}